package com.cct.digitalarchapi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.cct.digitalarchapi.common.CommonUtil;
import com.cct.digitalarchapi.common.Response;
import com.cct.digitalarchapi.pojo.vo.AllMenuVo;
import com.cct.digitalarchapi.service.RoleAndMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @FileName RoleAndMenuController
 * @Description 角色菜单控制器
 * @Author Created by chenmingzhe
 * @Date 2024/2/20
 **/
@Slf4j
@RestController
@RequestMapping(value = "/roleMenu")
public class RoleAndMenuController {
    @Autowired
    private RoleAndMenuService roleAndMenuService;
    @Autowired
    private CommonUtil commonUtil;
    /**
     *新增角色
     */
    @RequestMapping(value="/addRole",method= RequestMethod.POST)
    public Response addRole(@RequestBody JSONObject obj){
        //角色名
        String roleName=obj.getString("roleName");
        //菜单ID列表，如[1,2,3]
        List<Integer> menuList=obj.getList("menuList",Integer.class);
        commonUtil.recordOperateLog("新增角色名为"+roleName+"的角色");
        return roleAndMenuService.addRole(roleName,menuList);
    }
    /**
     *修改角色名或菜单
     */
    @RequestMapping(value="/updateRole",method= RequestMethod.POST)
    public Response updateRole(@RequestBody JSONObject obj) {
        //角色id
        int roleId=obj.getInteger("roleId");
        //角色名
        String roleName=obj.getString("roleName");
        //菜单ID列表，如[1,2,3]
        List<Integer> menuList=obj.getList("menuList",Integer.class);
        commonUtil.recordOperateLog("修改角色id为"+roleId+"的角色名为"+roleName);
        return roleAndMenuService.updateRole(roleId,roleName,menuList);
    }
    /**
     *删除角色及菜单
     */
    @RequestMapping(value="/deleteRole",method= RequestMethod.POST)
    public Response deleteRole(@RequestBody JSONObject obj) {
        int roleId=obj.getInteger("roleId");
        commonUtil.recordOperateLog("删除角色id为"+roleId+"的角色");
        return roleAndMenuService.deleteRole(roleId);
    }
    /**
     *获取所有菜单
     */
    @RequestMapping(value="/getAllMenu",method= RequestMethod.POST)
    public Response getAllMenu() {
        try {
            List<AllMenuVo> menuList=roleAndMenuService.getAllMenu("1",0);
            return Response.success(menuList);
        }catch (Exception e){
            e.printStackTrace();
            log.error("获取所有菜单失败：{}", e.getMessage());
            return Response.error("获取所有菜单失败");
        }

    }

    /**
     *上传、更新菜单的图标
     */
    @RequestMapping(value="/updateMenuIcon",method= RequestMethod.POST)
    public Response uploadWaterMark(@RequestBody JSONObject obj) {
        int menuId=obj.getInteger("menuId");
        String iconPath=obj.getString("iconPath");
        commonUtil.recordOperateLog("更新菜单id为"+menuId+"的菜单图标");
        return roleAndMenuService.updateMenuIcon(iconPath,menuId);
    }

    /**
     *获取所有角色
     */
    @RequestMapping(value="/getAllRole",method= RequestMethod.POST)
    public Response getAllRole(){
        return roleAndMenuService.getAllRole();
    }

    /**
     *通过角色获取菜单
     */
    @RequestMapping(value="/getMenuByRole",method= RequestMethod.POST)
    public Response getMenuByRole(@RequestBody JSONObject obj){
        int roleId=obj.getInteger("roleId");
        return roleAndMenuService.getMenuByRole(roleId);
    }
}
